package org.video.mine;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;

import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlobalStr {
    public static String path = Environment.getExternalStorageDirectory() + "/movieApp/";
    public static String saveType = ".mp4";
    public static Context context;
    public static Handler handler;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        ViewDataBinding titleBinding = null;

        public ViewHolder(ViewDataBinding titleBinding) {
            super(titleBinding.getRoot());
            this.titleBinding = titleBinding;
        }

        public ViewDataBinding getTitleBinding(){
            return titleBinding;
        }
    }

    public static String getSDPath(Context context) {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);// 判断sd卡是否存在
        if (sdCardExist) {
            if (Build.VERSION.SDK_INT>=29){
//Android10之后
                sdDir = context.getExternalFilesDir(null);
            }else {
                sdDir = Environment.getExternalStorageDirectory();// 获取SD卡根目录
            }
        } else {
            sdDir = Environment.getRootDirectory();// 获取跟目录
        }
        sdDir = Environment.getExternalStorageDirectory();// 获取SD卡根目录
        return sdDir.toString() + "/movieApp/";
    }

    /**
     * 转换文件大小
     * @param fileS
     * @return
     */
    public static String formetFileSize(long fileS)
    {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize="0B";
        if(fileS==0){
            return wrongSize;
        }
        if (fileS < 1024){
            fileSizeString = df.format((double) fileS) + "B";
        }
        else if (fileS < 1048576){
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        }
        else if (fileS < 1073741824){
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        }
        else{
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        return fileSizeString;
    }

    public static boolean isUrl(String str) {
        if (TextUtils.isEmpty(str))
            return false;
        str = str.trim();
        return str.matches("^(http|https)://.+");
    }

    public static String returnName(String name){
        if (!TextUtils.isEmpty(name)) {
            String regEx="[:/(\\\\)(\\?)<>\"(\\|)]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(name);
            return m.replaceAll("");
        }
        return null;
    }
}
